<!DOCTYPE html>
<html>

<head>
  <title>jQuery XPath plugin test page</title>
  <style type="text/css">
    body {
      font-family: Arial;
      font-size: 0.8em;
    }

    p {
      margin-top: 0.5em;
      margin-bottom: 0.5em;
    }

    html,
    html body {
      width: 100%;
      height: 100%;
    }

    .ab {
      height: 100%;
    }

    iframe {
      width: 100%;
      height: 80%;
    }
  </style>
</head>

<body>
  <button id="addSelect">开始添加ABTest</button>
  <button id="addStyles">应用ABTest</button>
  <input value="/test/test-path2.html" id="ip-url" />
  <button id="btn-go"> go </button>
  <div class="ab">
    <iframe id="iframe" src="./test-path2.html">
    </iframe>
  </div>

  <script type="text/javascript">
    function loadJS(doc, url, callback) {

      var script = doc.createElement('script'),

        fn = callback || function () {};

      script.type = 'text/javascript';



      //IE

      if (script.readyState) {

        script.onreadystatechange = function () {

          if (script.readyState == 'loaded' || script.readyState == 'complete') {

            script.onreadystatechange = null;

            fn();

          }

        };

      } else {

        //其他浏览器

        script.onload = function () {

          fn();

        };

      }

      script.src = url;

      doc.getElementsByTagName('head')[0].appendChild(script);

    }

    // loadJS(document,'./js/myXpath.js',function(){
    //         console.log('finish 1');
    //     });

    document.getElementById('addSelect').addEventListener('click', function () {
      var obj = document.getElementById("iframe").contentWindow.document;
      loadJS(obj, './js/myXpath.js', function () {
        console.log('finish 1');
      });
    });

    document.getElementById('addStyles').addEventListener('click', function () {
      var obj = document.getElementById("iframe").contentWindow.document;
      loadJS(obj, './js/myNewVersion.js', function () {
        console.log('finish 2');
      });
    });

    document.getElementById('btn-go').addEventListener('click', function () {
      document.getElementById("iframe").src = document.getElementById('ip-url').value;
    });
  </script>

</body>

</html>